﻿using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.UI;
using FwProject.Com.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace FwProject.Com
{
    public class FootprintAppService : FwProjectAppServiceBase, IFootprintAppService
    {
        private readonly IRepository<Footprint, Guid> _footprintRepository;
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;
        private readonly IRepository<ShopOrderDetails, Guid> _shopOrderDetailsRepository;

        public FootprintAppService(IRepository<Footprint, Guid> footprintRepository, IRepository<ShopProduct, Guid> repositoryShopProduct, IRepository<ShopOrderDetails, Guid> shopOrderDetailsRepository)
        {
            _footprintRepository = footprintRepository;
            _repositoryShopProduct = repositoryShopProduct;
            _shopOrderDetailsRepository = shopOrderDetailsRepository;
        }

        /// <summary>
        /// APP-获取登陆会员的足迹分页信息（根据访问时间分组）
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">页数</param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<IPagedList<FootprintOutputDto>> GetLogMemberFootprintsAsync(int page, int pageSize)
        {
            var userid = AbpSession.UserId;
            if (!userid.HasValue)
                throw new UserFriendlyException(2, "请先登陆！");

            var query = _footprintRepository.GetAll().Where(o => o.UserId == userid.Value).Join(_repositoryShopProduct.GetAll(), i => i.ShopProductId, j => j.Id, (i, j) => new
            {
                i.CreationTime,
                ShopProductId = j.Id,
                j.ProductName,
                j.SpcemarketPrice,
                j.ImagePic
            }).GroupJoin(_shopOrderDetailsRepository.GetAll().Where(o => o.DeliveryStatus == DeliveryStatus.已发货),i=>i.ShopProductId,j=>j.ShopproductId,(i,j)=>new {
                i.CreationTime,
                i.ShopProductId,
                i.ProductName,
                i.SpcemarketPrice,
                i.ImagePic,
                SoldCount=j.Count()
            }).GroupBy(o => o.CreationTime).Select(o => new FootprintOutputDto
            {
                CreationTime = o.Key,
                FootprintShopProducts = o.Select(f => new FootprintShopProductDto
                {
                    ShopProductId = f.ShopProductId,
                    ShopProductImg = f.ImagePic,
                    ShopProductName = f.ProductName,
                    ShopProductPrice = f.SpcemarketPrice,
                    SoldCount=f.SoldCount
                }).ToList()
            });

            return await query.OrderByDescending(o => o.CreationTime).ToPagedListAsync(page, pageSize);
        }

        [AbpAuthorize]
        public async Task DeleteFootprint(Guid Id)
        {
            var model = _footprintRepository.Get(Id);
            await _footprintRepository.DeleteAsync(model);
        }
        [AbpAuthorize]
        public async Task DeleteAllFootprint()
        {
            var userid = AbpSession.UserId;
            if (!userid.HasValue)
            {
                var model = _footprintRepository.GetAll();
                foreach (var item in model)
                {
                 await   _footprintRepository.DeleteAsync(item);
                }
            }
               
        }
    }
}
